package com.wingscode.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.Query;
import com.wingscode.modules.common.dao.AdvertisementDao;
import com.wingscode.modules.common.entity.AdvertisementEntity;
import com.wingscode.modules.common.service.AdvertisementService;
import com.wingscode.modules.common.service.UserService;
import com.wingscode.modules.sys.service.SysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("advertisementService")
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementDao, AdvertisementEntity> implements AdvertisementService {
    @Autowired
    private UserService userService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String title = "", userName = "", startTime = "", endTime = "";
        Integer type = null, dicId = null;

        if (params.containsKey("title"))
            title = (String) params.get("title");
        if (params.containsKey("userName"))
            userName = (String) params.get("userName");
        if (params.containsKey("startTime"))
            startTime = (String) params.get("startTime");
        if (params.containsKey("endTime"))
            endTime = (String) params.get("endTime");
        if (params.containsKey("type") && params.get("type") != null)
            type = Integer.parseInt(params.get("type").toString());
        if (params.containsKey("dicId") && params.get("dicId") != null)
            dicId = Integer.parseInt(params.get("dicId").toString());

        IPage<AdvertisementEntity> page = this.baseMapper.getList(new Query<AdvertisementEntity>().getPage(params), title, userName,
                startTime, endTime, type, dicId);

        return new PageUtils(page);
    }

    @Override
    public List<AdvertisementEntity> getAdvertisementList(int type) {
        Map<String, Object> map = new HashMap<>();
        map.put("limit", "5");
        IPage<AdvertisementEntity> page = this.page(
                new Query<AdvertisementEntity>().getPage(map),
                new QueryWrapper<AdvertisementEntity>().eq("type", 1).eq("status", type)
        );
        return page.getRecords();
    }

    @Override
    public void delete(Long[] ids){
        this.removeByIds(Arrays.asList(ids));
    }

}
